// --- Copyright (c) 2006-2008 Stefan Kyntchev ---
// This software is written and copyrighted by Stefan Kyntchev 
// and BeyondPod Team members. All rights are reserved.
// Author contact: support@beyondpod.mobi
// ------------------------------------------------------------------------
// This file is part of BeyondPod RSS Feed Reader and Podcast manager
// (www.codeplex.com/BeyondPod) 
// BeyondPod is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// BeyondPod is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more details. 
//  
// You should have received a copy of the GNU General Public License
// along with BeyondPod. If not, see <http://www.gnu.org/licenses/>
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System.Xml;
using svs.Mobile.BeyondMedia.FeedCore.RSS.Entities;

namespace svs.Mobile.BeyondMedia.FeedCore.RSS.Parsers
{
	internal class AtomEntryParser : ParserBase
	{
		private readonly RssFeedItem m_Item;
		private readonly string m_EnclosureDownloadPath;
		private readonly bool m_ForceUniqueEnclosureNames;


		public AtomEntryParser(string i_EnclosureDownloadPath, bool i_forceUniqueNames, RssFeed feed, RssFeedParser.FeedParseLevel parseLevel)
		{
			m_handlers["title"] = delegate(XmlReader aReader) { if(string.IsNullOrEmpty(m_Item.Title)) m_Item.Title = ReadElementString(aReader); };
			m_handlers["link"] = LinkHandler;
			m_handlers["published"] = delegate(XmlReader aReader)
			{
				string orgDate;
				m_Item.PubDate = ReadDateTime(aReader, out orgDate);
				m_Item.OriginalPubDateString = orgDate;
			};

			m_EnclosureDownloadPath = i_EnclosureDownloadPath;
			m_ForceUniqueEnclosureNames = i_forceUniqueNames;

			if (parseLevel == RssFeedParser.FeedParseLevel.Complete)
			{
				m_handlers["summary"] = DescriptionHandler;
				m_handlers["content"] = DescriptionHandler;

				m_handlers["name"] = AutorHandler;
			}

			m_Item = new RssFeedItem(feed);
		}

		public RssFeedItem Item
		{
			get { return m_Item; }
		}


		private void DescriptionHandler(XmlReader aReader)
		{
			if (string.IsNullOrEmpty(m_Item.Description))
				m_Item.Description = ReadElementString(aReader);
		}

		private void LinkHandler(XmlReader aReader)
		{
			var att = ReadAttributes(aReader);

			if (att.ContainsKey("href") && (!att.ContainsKey("rel") || (att.ContainsKey("rel") && att["rel"] == "alternate")))
			{
				m_Item.Link = att["href"];
			}

			if (att.ContainsKey("href") && (att.ContainsKey("rel") && att["rel"] == "enclosure"))
			{
				var e = new RssEnclosure(m_EnclosureDownloadPath, m_ForceUniqueEnclosureNames) {Url = att["href"]};

				if (att.ContainsKey("length"))
				{
					e.Length = ReadInt(att["length"]);
				}

				if (att.ContainsKey("type"))
					e.Type = att["type"];

				m_Item.Enclosure = e;
			}
		}

		private void AutorHandler(XmlReader aReader)
		{
			m_Item.Author = ReadElementString(aReader);
		}
	}
}